package mt.interfaceGui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import mt.Order;
import mt.comm.ClientComm;
import mt.dummy.ClientCommDummy;

public class SentinelaVender implements ActionListener {
	private GUI gui;
	private ConfiguracaoTabela tableModel;
	private String nomeUtilizador;
	private ClientCommDummy clientComm;
	private JFrame frameVender;

	public SentinelaVender(GUI gui, ConfiguracaoTabela tableModel, String nomeUtilizador, ClientComm clientComm) {
		this.gui=gui;
		this.tableModel=tableModel;
		this.nomeUtilizador=nomeUtilizador;
		this.clientComm=(ClientCommDummy) clientComm;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		
		 frameVender= new JFrame("Vender");
		frameVender.setSize(300,300);
		frameVender.setLocationRelativeTo(null);
		frameVender.setResizable(false);
		JButton done = new JButton("Done");


		JLabel nome = new JLabel("User: "+nomeUtilizador);

		JLabel stock = new JLabel("Insert Stock");
		final JTextField stockField= new JTextField();

		JLabel unit = new JLabel("Insert Unit");
		final JTextField unitField= new JTextField();

		JLabel price = new JLabel("Insert Price");
		final JTextField priceField= new JTextField();


		frameVender.setLayout(new BorderLayout());
		JPanel panel1= new JPanel();
		panel1.setLayout(new GridLayout(7,1));

		panel1.add(nome);
		panel1.add(stock);
		panel1.add(stockField);
		panel1.add(unit);
		panel1.add(unitField);
		panel1.add(price);
		panel1.add(priceField);
		
		frameVender.add(panel1);
		frameVender.add(done, BorderLayout.SOUTH);
		done.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				int unit1= Integer.parseInt(unitField.getText());
				double price1= Double.parseDouble(priceField.getText());
				Order order = Order.createSellOrder(nomeUtilizador, stockField.getText(), unit1, price1);
				//tableModel.add(order);
				clientComm.sendOrder(order);
				frameVender.setVisible(false);
				
				
			}
		});
		
		
		frameVender.setVisible(true);

		
		
	
		
		

	}

}
